<?php
namespace app\wx\controller;
use think\Controller;
use think\Request;
use think\config;

 use think\Db;
class Index3 extends Controller
{

	/**
	 * 微信授权演示
	 */

    public function index(){
		// http://www.hyhthjk.com.cn/note.php/wx/Index3/index
		return $this->fetch();
	}





    public function person(Request $request)
    {

    // 1 获取到code
    	//接收这个地址发来的 code 信息 https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx9aacf08f3fbd2e37&redirect_uri=http://www.bjlya.com/kehuweixiu/Login/person&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
   $code = input('get.code');
   $appid = 'wx9aacf08f3fbd2e37';  //老板商城的
  $secret = '821d788d0fe0d79b5ae414d1a0b93325';   //老板商城的	
        //用 code 换取票据
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
    
       $data = file_get_contents($url); 
       $data = json_decode($data,true);  //加 true 使json 转成数组 
       //从data中 获得 $access_token  openid 然后组成下面的地址
      
       $access_token = $data['access_token'];
      
        $openid = $data['openid'];
       $url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
       $data = file_get_contents($url); 
       dump($data);
       // 3. 拉取用户openid
       //转成数组
       $data = json_decode($data,true);  //加 true 使json 转成数组 
      // dump($data['openid']);
     // 查询kehu 表 如果 灭有这个人 就新增记录
		$table = 'note_kehu';
		$where['openid'] = $data['openid'];
		$res = $this->select1($table,$where);
		echo Db::table('note_kehu')->getlastsql();  // model 下获得最后一条sql
		// dump($res);
		if($res == null){
			 // echo '<br/>要注册';
			 
			$where['nickname'] = $data['nickname'];
			$where['address'] = $data['country'].$data['province'].$data['city'];
			$where['headimgurl'] = $data['headimgurl'];
			$where['laiyuan'] = '微信';
			$where['quanxian'] = '客户';
			$where['downNumber'] = 0;
			$this->insert1($table,$where);

     
}
	

		}
	/***
   * 下面是model 区
   */
	public function select1($table,$where){
		return Db::table($table)->where($where)->select();		
	}


	public function insert1($table,$where){
	//	echo'model';
	//	dump($table);
	    return Db::table($table)->insert($where);
	}
}
